<?php

namespace app\job;

use app\index\model\Expercard;
use app\index\model\Item;
use app\index\model\Miaosha;
use app\index\model\Order;
use app\index\model\Servicenews;
use app\index\model\Vipcard;
use app\util\Redpacket;
use app\util\Utils;
use app\index\model\Store;
use Predis\Client;
use think\Db;
use think\queue\Job;
use app\index\model\Qianggou;
use app\index\model\Ushare;

/*
 * 队列
 */

class Queue
{
    /*
     * 测试队列方法
     */
    public function dotestFunc(Job $job, $data)
    {
        $isJobDone = $this->dotest($data);
        if ($isJobDone) {
            //成功删除任务
            $job->delete();
        } else {
            //任务轮询4次后删除
            if ($job->attempts() > 3) {
                // 第1种处理方式：重新发布任务,该任务延迟10秒后再执行
                //$job->release(10);
                // 第2种处理方式：原任务的基础上1分钟执行一次并增加尝试次数
                //$job->failed();
                // 第3种处理方式：删除任务
                $job->delete();
            }
        }
    }

    /*
     * 测试
     */
    public function dotest($data)
    {
        Db::name('aaa')->insert($data);

    }

    /*
     * 发送回调消息队列方法
     */
    public function sendNotifyMessageFunc(Job $job, $data)
    {
        $isJobDone = $this->sendNotifyMessage($data);
        if ($isJobDone) {
            //成功删除任务
            $job->delete();
        } else {
            //任务轮询4次后删除
            if ($job->attempts() > 3) {
                // 第1种处理方式：重新发布任务,该任务延迟10秒后再执行
                //$job->release(10);
                // 第2种处理方式：原任务的基础上1分钟执行一次并增加尝试次数
                //$job->failed();
                // 第3种处理方式：删除任务
                $job->delete();
            }
        }
    }

    /*
     * 发送回调消息
     */
    private function sendNotifyMessage($data)
    {
        $keyArray = array_keys($data);
        $key = $keyArray[0];
        $value = $data[$key];

        if ($key == Servicenews::TITLEID_1) {
            $order_id = $value['order_id'];

            $orderRow = Order::with('ordPro,custom,store')
                ->where('id', '=', $order_id)
                ->field('id, store_id, ctm_id, staff_id, starttime, pay_money, add_time,ordtype,vip_money')
                ->order('add_time DESC')
                ->find();

            if (!is_null($orderRow)) {
                $store_id = $orderRow['store_id'];
                if($orderRow['ordtype'] == 2){
                    $pay_money = $orderRow['vip_money'];
                }  else {
                    $pay_money = $orderRow['pay_money'];
                }
                

                //订单产品信息
                $pro_id = $orderRow['ord_pro']['pro_id'];
                $number = $orderRow['ord_pro']['number'];
                $type = $orderRow['ord_pro']['type'];

                //用户信息
                $custom_phone = $orderRow['custom']['custom_phone'];
                $custom_name = $orderRow['custom']['custom_name'];

                //产品信息
                if ($type == Order::TYPE_2) {
                    $item_name = Item::where('id', '=', $pro_id)->value('item_name');
                } else if ($type == Order::TYPE_3) {
                    $item_name = Vipcard::where('id', '=', $pro_id)->value('vip_name');
                } else if ($type == Order::TYPE_4) {
                    $item_name = Expercard::where('id', '=', $pro_id)->value('name');
                } else if ($type == Order::TYPE_5) {
                    $miaoshaRow = Miaosha::with('item')->where('id', '=', $pro_id)->find();
                    $item_name = $miaoshaRow['item']['item_name'];
                }else if ($type == Order::TYPE_6) {
                    $miaoshaRow = Qianggou::with('item')->where('id', '=', $pro_id)->find();
                    $item_name = $miaoshaRow['item']['item_name'];
                }else if ($type == Order::TYPE_7) {
                    $miaoshaRow = Ushare::with('item')->where('id', '=', $pro_id)->find();
                    $item_name = $miaoshaRow['item']['item_name'];
                }

                //给店铺发推送
                $staffRow = Store::getStoreStaff($store_id);
                $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                $content = "【已付款】[{$custom_name}] [{$item_name}] X [{$number}]，共计 [{$pay_money}] 元";
                $msgContent = Utils::getColoredString($content);
                $pushContent = Utils::getPushString($content);
                $alias = $staffRow['staff']['telephone'] . $store_id;
                $userid = $staffRow['staff_id'];

                Utils::sendServicenews($key, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_1, $store_id, [
                    'title' => '订单消息',
                    'content' => $pushContent,
                    'alias_type' => Servicenews::ALIAS_TYPE_1,
                    'alias' => $alias,
                ],'order');

                //给店铺发短信
//                $smsContent = "您有一个新的店铺订单，顾客姓名：{$custom_name}，顾客电话：{$custom_phone}，已成功购买您的店铺中的“{$item_name}”。";
//                Utils::sendSms($staffRow['staff']['telephone'], $smsContent);
            }
        } else if ($key == Servicenews::TITLEID_2) {
            $order_id = $value['order_id'];
            $staff_type = $value['staff_type'];
            $yuyue_id = $value['yuyue_id'];

            $orderRow = Order::with('ordPro,custom,store,staff')
                ->where('id', '=', $order_id)
                ->field('id, store_id, ctm_id, staff_id, starttime, pay_money, add_time')
                ->order('add_time DESC')
                ->find();

            if (!is_null($orderRow)) {
                $store_id = $orderRow['store_id'];
                $staff_id = $orderRow['staff_id'];
                $starttime = $orderRow['starttime'];

                //订单产品信息
                $pro_id = $orderRow['ord_pro']['pro_id'];
                $number = $orderRow['ord_pro']['number'];
                $type = $orderRow['ord_pro']['type'];

                //用户信息
                $custom_phone = $orderRow['custom']['custom_phone'];
                $custom_name = $orderRow['custom']['custom_name'];

                //产品信息
                if ($type == Order::TYPE_2) {
                    $item_name = Item::where('id', '=', $pro_id)->value('item_name');
                } else if ($type == Order::TYPE_3) {
                    $item_name = Vipcard::where('id', '=', $pro_id)->value('vip_name');
                } else if ($type == Order::TYPE_4) {
                    $item_name = Expercard::where('id', '=', $pro_id)->value('name');
                } else if ($type == Order::TYPE_5) {
                    $miaoshaRow = Miaosha::with('item')->where('id', '=', $pro_id)->find();
                    $item_name = $miaoshaRow['item']['item_name'];
                } else if ($type == Order::TYPE_6) {
                    $miaoshaRow = Qianggou::with('item')->where('id', '=', $pro_id)->find();
                    $item_name = $miaoshaRow['item']['item_name'];
                }else if ($type == Order::TYPE_7) {
                    $miaoshaRow = Ushare::with('item')->where('id', '=', $pro_id)->find();
                    $item_name = $miaoshaRow['item']['item_name'];
                }

                //美容师信息
                $staff_name = $orderRow['staff']['staff_name'];


                $yuyue_time = date('Y-m-d H:i', $starttime);
                $custom_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                if ($staff_type == 1) {
                    $staffRow = Store::getStoreStaff($store_id);
                    $content = "【已预约】[{$custom_name}] 预约了 [{$yuyue_time}] [{$staff_name}] 的 [{$item_name}]，快去确认吧";
                    $msgContent = Utils::getColoredString($content);
                    $pushContent = Utils::getPushString($content);
                    $alias = $staffRow['staff']['telephone'] . $store_id;
                    $userid = $staffRow['staff_id'];
                    Utils::sendServicenews($key, $order_id, $yuyue_id, $msgContent, $userid, Servicenews::TYPE_1, $store_id, [
                        'title' => '预约消息',
                        'content' => $pushContent,
                        'alias_type' => Servicenews::ALIAS_TYPE_1,
                        'alias' => $alias,
                    ]);

                } else if ($staff_type == 2) {
                    $content = "【已预约】[{$custom_name}] [{$yuyue_time}] 向您预约了 [{$item_name}] 服务";
                    $msgContent = Utils::getColoredString($content);
                    $pushContent = Utils::getPushString($content);
                    $alias = $orderRow['staff']['telephone'] . $store_id;
                    $userid = $staff_id;
                    Utils::sendServicenews($key, $order_id, $yuyue_id, $msgContent, $userid, Servicenews::TYPE_1, $store_id, [
                        'title' => '预约消息',
                        'content' => $pushContent,
                        'alias_type' => Servicenews::ALIAS_TYPE_1,
                        'alias' => $alias,
                    ],'server');

                }
            }

        } else if ($key == Servicenews::TITLEID_4) {
            //pass

        } else if ($key == Servicenews::TITLEID_5) {
            //pass

        } else if ($key == Servicenews::TITLEID_7) {
            //pass

        } else if ($key == Servicenews::TITLEID_9) {
            //pass

        } else if ($key == Servicenews::TITLEID_10) {
            $order_id = $value['order_id'];

            $orderRow = Order::where('id', '=', $order_id)
                ->field('id, store_id, pay_money')
                ->order('add_time DESC')
                ->find();

            if (!is_null($orderRow)) {
                $store_id = $orderRow['store_id'];
                $pay_money = $orderRow['pay_money'];

                //给店铺发推送
                $staffRow = Store::getStoreStaff($store_id);
                $content = "【新增收入】最新收入 [{$pay_money}] 元，请注意查收";
                $msgContent = Utils::getColoredString($content);
                $pushContent = Utils::getPushString($content);
                $alias = $staffRow['staff']['telephone'] . $store_id;
                $userid = $staffRow['staff_id'];

                Utils::sendServicenews($key, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_1, $store_id, [
                    'title' => '收入消息',
                    'content' => $pushContent,
                    'alias_type' => Servicenews::ALIAS_TYPE_1,
                    'alias' => $alias,
                ]);
            }
        } else if ($key == Servicenews::TITLEID_11) {
            //pass

        } else if ($key == Servicenews::TITLEID_12) {
            //pass

        } else if ($key == Servicenews::TITLEID_13) {
            //pass

        }

    }

    /*
     * 写入日志队列方法
     */
    public function insertRequestLogFunc(Job $job, $data)
    {
        $isJobDone = $this->insertRequestLog($data);
        if ($isJobDone) {
            //成功删除任务
            $job->delete();
        } else {
            //任务轮询4次后删除
            if ($job->attempts() > 3) {
                // 第1种处理方式：重新发布任务,该任务延迟10秒后再执行
                //$job->release(10);
                // 第2种处理方式：原任务的基础上1分钟执行一次并增加尝试次数
                //$job->failed();
                // 第3种处理方式：删除任务
                $job->delete();
            }
        }
    }

    /*
     * 写入日志
     */
    public function insertRequestLog($data)
    {
        Db::name('request_log')->insert($data);

    }

    /*
     * 抢红包方法
     */
    public function getRedpacketFunc(Job $job, $data)
    {
        $isJobDone = $this->getRedpacket($data);
        if ($isJobDone) {
            //成功删除任务
            $job->delete();
        } else {
            //任务轮询4次后删除
            if ($job->attempts() > 3) {
                // 第1种处理方式：重新发布任务,该任务延迟10秒后再执行
                //$job->release(10);
                // 第2种处理方式：原任务的基础上1分钟执行一次并增加尝试次数
                //$job->failed();
                // 第3种处理方式：删除任务
                $job->delete();
            }
        }
    }

    /*
     * 抢红包
     */
    public function getRedpacket($data)
    {
        $ctm_id = $data['ctm_id'];
        $store_id = $data['store_id'];
        $redpacket_id = $data['redpacket_id'];

        $row = Db::name('redpacket')
            ->where('id', '=', $redpacket_id)
            ->field('id, number, send_count, last_money')
            ->find();
        if (!is_null($row)) {
            $number = $row['number'];
            $send_count = $row['send_count'];
            $last_money = $row['last_money'];

            $count = $number - $send_count;

            $redpacket = new Redpacket();
            $list = $redpacket->splitRedPackets($last_money, $count);
            $red_money = $list[0];

            Db::startTrans();

            $redCtmData = [
                'store_id' => $store_id,
                'custom_id' => $ctm_id,
                'redpacket_id' => $redpacket_id,
                'money' => $red_money,
                'add_time' => time(),
            ];
            $redCtmResult = Db::name('redpacket_custom')->insert($redCtmData);

            $send_count = $send_count + 1;
            $last_money = bcsub($last_money, $red_money, 2);
            $redData = [
                'send_count' => $send_count,
                'last_money' => $last_money,
            ];
            $redResult = Db::name('redpacket')->where('id', '=', $redpacket_id)->update($redData);

            $customRow = Db::name('custom')->where('id', '=', $ctm_id)->field('red_money')->find();
            $customResult = Db::name('custom')->where('id', '=', $ctm_id)->update([
                'red_money' => bcadd($customRow['red_money'], $red_money, 2),
            ]);

            //入库操作
            if ($redCtmResult && $redResult && $customResult) {
                Db::commit();

            } else {
                Db::rollback();
            }

        }

    }




}